package com.lc.qianrenshiye.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.GoodsTypeGet;
import com.lc.qianrenshiye.uitls.MyToast;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/10.
 */
public class MyIndicator extends LinearLayout {

    /**
     * 标题正常时的颜色
     */
    private static final int COLOR_TEXT_NORMAL = 000000;
    /**
     * 标题选中时的颜色
     */
    private static final int COLOR_TEXT_HIGHLIGHTCOLOR = 999999;
    /**
     * 手指滑动时的偏移量
     */
    private float mTranslationX;

    private MyIndicatorOnclickListener myIndicatorOnclickListener;

    private List<GoodsTypeGet.GoodsType> mTabTitles;

    public List<Integer> moveX = new ArrayList<>();

    public int totalX;

    public MyIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.myIndicatorOnclickListener = (MyIndicatorOnclickListener) context;

    }

    /**
     * 设置tab的标题内容 可选，可以自己在布局文件中写死
     *
     * @param datas
     */
    public void setTabItemTitles(List<GoodsTypeGet.GoodsType> datas)
    {
        // 如果传入的list有值，则移除布局文件中设置的view
        if (datas != null && datas.size() > 0)
        {
            this.removeAllViews();
            this.mTabTitles = datas;

            for (GoodsTypeGet.GoodsType title : mTabTitles)
            {
                // 添加view
                addView(generateTextView(title.title));
            }
            // 设置item的click事件
            setItemClickEvent();
        }

    }

    /**
     * 根据标题生成我们的TextView
     *
     * @param text
     * @return
     */
    private TextView generateTextView(String text)
    {
        TextView tv = new TextView(getContext());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, ScaleScreenHelperFactory.getInstance().getWidthHeight(68));

        lp.setMargins(ScaleScreenHelperFactory.getInstance().getWidthHeight(9), 0, ScaleScreenHelperFactory.getInstance().getWidthHeight(9), 0);
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(getResources().getColor(R.color.black_arrow_color));
        tv.setText(text);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, ScaleScreenHelperFactory.getInstance().getWidthHeight(24));
        tv.setLayoutParams(lp);

        tv.measure(0, 0);

        totalX += tv. getMeasuredWidth() + ScaleScreenHelperFactory.getInstance().getWidthHeight(18);

        moveX.add(totalX);

        return tv;
    }


    /**
     * 设置点击事件
     */
    public void setItemClickEvent()
    {
        int cCount = getChildCount();
        for (int i = 0; i < cCount; i++)
        {
            final int j = i;
            View view = getChildAt(i);

            view.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    myIndicatorOnclickListener.onclick(j);
                }
            });
        }
    }

    /**
     * 高亮文本
     *
     * @param position
     */
    public void highLightTextView(int position)
    {
        View view = getChildAt(position);
        if (view instanceof TextView)
        {
            ((TextView) view).setTextColor(getResources().getColor(R.color.red_color));
        }

    }

    /**
     * 重置文本颜色
     */
    public void resetTextViewColor(int position)
    {
            View view = getChildAt(position);
            if (view instanceof TextView)
            {
                ((TextView) view).setTextColor(getResources().getColor(R.color.black_text_color));
            }
    }

    /**
     * 重置文本颜色
     */
    public void resetTextViewColor()
    {
        for (int i = 0; i < getChildCount(); i++)
        {
            View view = getChildAt(i);
            if (view instanceof TextView)
            {
                ((TextView) view).setTextColor(getResources().getColor(R.color.black_text_color));
            }
        }
    }


    public interface MyIndicatorOnclickListener {

        void onclick(int pos);
    }


}
